package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;


import java.util.List;
import java.util.concurrent.CompletableFuture;

@Cmd(code = GameProtocol.S_TOMB_EVENT_TAKE_CARE_SWITCH_UPDATE,desc = "修改自动处理开关")
public class TombUpdateAutoSwitchCmd extends AbstractCommand {

    private List<Integer> openSwitchList = null;

    private Tomb.TombUpdateAutoEventSwitchReqMsg reqMsg;

    private Tomb.TombUpdateAutoEventSwitchRespMsg.Builder respMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = Tomb.TombUpdateAutoEventSwitchReqMsg.parseFrom(packet.getBytes());
        openSwitchList = reqMsg.getOpenSwitchTypeList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respMsg = Tomb.TombUpdateAutoEventSwitchRespMsg.newBuilder();
        //常规判断
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.respMsg.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_EVENT_TAKE_CARE_SWITCH_UPDATE, this.respMsg);
            return;
        }
        int ret = player.getModule(TombModule.class).isOpen(true);
        if (ret > 0) {
            this.respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOMB_EVENT_TAKE_CARE_SWITCH_UPDATE, this.respMsg);
            return;
        }
        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(), CrossProtocol.C_TOMB_UPDATE_AUTO_SWITCH_TO_CROSS, activityInfo.getActivityId(), reqMsg.toBuilder(), respMsg);
        future.thenAccept(syncResponse -> {
            if (!this.respMsg.hasRet()){
                this.respMsg.setRet(0);
            }
            player.sendPacket(ClientProtocol.U_TOMB_EVENT_TAKE_CARE_SWITCH_UPDATE,this.respMsg);


        });
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
